<template>
  <div class="warpper-box businessOrder">
    <enterprise-select v-if="userInfo.type == 4" ref="enterprise"
      @enterpriseChange="enterpriseChange"></enterprise-select>
    <a-card style="margin-top: 10px;">
      <a-form layout="inline">
        <a-form-item label="月份选择">
          <a-month-picker valueFormat="YYYY-MM" placeholder="选择月份" v-model="params.insureDate"></a-month-picker>
        </a-form-item>
        <a-form-item label="结算状态">
          <a-select v-model="params.orderStatus" placeholder="选择结算状态" style="width: 160px;">
            <a-select-option key="1">待提交</a-select-option>
            <a-select-option key="2">待支付</a-select-option>
            <a-select-option key="3">已支付</a-select-option>
            <a-select-option key="4">已拒绝</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" icon="search" @click="loadTable(true)">搜索</a-button>
          <a-button type="primary" icon="setting" @click="reset()">重置</a-button>
          <a-button type="primary" @click="downloadOrder">导出</a-button>
        </a-form-item>
      </a-form>
      <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
        @change="handleTableChange" rowKey="insureNo" style="margin-top: 10px;">
        <!-- 操作栏-->
        <span slot="action" slot-scope="text, record">
          <!-- 编辑模块 | 字段 -->
          <a @click="$refs.createInfo.add(record.insureNo, record.orderStatus)">详情</a>
          <a-divider type="vertical" />
          <a @click="downloadDetail(record)">导出详情</a>
        </span>
      </a-table>
    </a-card>
    <add-info ref="createInfo" @on-ok="addLoad"></add-info>
  </div>
</template>

<script>
import store from '@/store'
import EnterpriseSelect from "@/components/human/enterpriseSelect.vue"
import {
  COLUMNS_BUSINESSORDER
} from "./form"
import AddInfo from "./businessOrderEnterprise_add.vue"
import {
  getEnterpriseTablePage,
  enterpriseExportInsureBusiness,
  enterpriseExportInsureBusinessStaff
} from "@/api/human/ensureBusinessOrder"
import {
  parkRelation
} from "@/api/human/ensurePayOrder"
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'businessOrder',
  components: {
    AddInfo,
    EnterpriseSelect
  },
  data() {
    return {
      params: {
        orderStatus: undefined,
        insureDate: '',
      },
      table: {
        columns: [
          {
            title: '参保月份',
            dataIndex: 'insureDate',
          },
          {
            title: '商保订单名称',
            dataIndex: 'insureName',
          },
          {
            title: '服务商',
            dataIndex: 'parkName'
          },
          {
            title: '商保缴纳总额',
            dataIndex: 'insurePayAmount',
          },
          {
            title: '缴纳人数',
            dataIndex: 'insureCount',
          },
          {
            title: '结算状态',
            dataIndex: 'orderStatus',
            customRender: (text, record, index) => {
              if (text == 1) {
                return "待提交"
              } else if (text == 2) {
                return "待支付"
              } else if (text == 3) {
                return "已支付"
              } else if (text == 4) {
                return "已拒绝"
              }
              return ""
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            fixed: "right",
            scopedSlots: { customRender: 'action' }
          },
        ],
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },

      createVisible: false,
      insureName: "", // 商保订单名称
      insureDate: "", // 参保月份
      parkId: undefined, // 服务商
      fileName: "",
      formData: undefined, // 生成保单参数

      enterpriseId: "", // 企业id
      enterpriseData: [], // 客户列表
      parkList: [], // 服务商列表

      selectedRowKeys: [], // 选择的table数据id
      selectedRows: [], // 选择的table数据
    }
  },
  filters: {

  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
    if (this.userInfo.type == 4) {
      this.$refs.enterprise.getEnterpriseData();
    } else {
      this.enterpriseId = this.userInfo.relationId;
      this.loadTable(true);
    }
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  methods: {
    // table复选框选择
    onSelectChange(selectedRowKeys, selectedRows) {
      // console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    // 获取列表
    async loadTable(flag) {
      let params = this.params;
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      params.enterpriseId = this.enterpriseId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = await getEnterpriseTablePage(params);
      if (code === '1') {
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    // 导出订单
    downloadOrder() {
      enterpriseExportInsureBusiness({
        enterpriseId: this.enterpriseId,
        orderStatus: this.params.orderStatus,
        insureDate: this.params.insureDate
      }).then(res => {
        const blob = new Blob([res]);
        const fileName = '商保管理订单.xlsx';
        const elink = document.createElement('a'); // 创建a标签
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click(); // 点击下载
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink); // 释放标签
      })
    },
    // 导出详情
    downloadDetail(row) {
      enterpriseExportInsureBusinessStaff({
        insureNo: row.insureNo
      }).then(res => {
        const blob = new Blob([res]);
        const fileName = '商保管理订单详情.xlsx';
        const elink = document.createElement('a'); // 创建a标签
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click(); // 点击下载
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink); // 释放标签
      })
    },
    // 企业选择回调
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.loadTable(true);
    },
    addLoad() {
      this.loadTable(true);
    },
    reset() {
      this.params.insureDate = '';
      this.params.orderStatus = undefined;
      this.loadTable(true);
    }
  }
}
</script>
<style lang="less">
.businessOrder {
  .ant-tabs-bar {
    margin-bottom: 5px;
  }
}
</style>